package Interfaz;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerListModel;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.SwingUtilities;

import Juego.JuegoCliente;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class InterfazDarDeAlta extends javax.swing.JFrame {
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JTextField nombre;
	private JLabel jLabel5;
	private JTextField ip;
	private ButtonGroup grupo;
	private JButton alta;
	private JRadioButton btn_b;
	private JRadioButton btn_a;
	private JSpinner puerto;
	private JCheckBox ultimo;
	

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				InterfazDarDeAlta inst = new InterfazDarDeAlta();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public InterfazDarDeAlta() {
		super();
		initGUI();
		this.getGrupo();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setBackground(new java.awt.Color(217,236,255));
			getContentPane().setLayout(null);
			this.setTitle("Darse de alta");
			this.setIconImage(new ImageIcon(getClass().getClassLoader().getResource("Imagenes/icono1.png")).getImage());
			this.setResizable(false);
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1, "Center");
				jLabel1.setText("Nombre:");
				jLabel1.setBounds(43, 51, 85, 29);
				jLabel1.setFont(new java.awt.Font("Segoe UI",1,16));
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("Puerto:");
				jLabel2.setBounds(43, 95, 77, 25);
				jLabel2.setFont(new java.awt.Font("Segoe UI",1,16));
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("Grupo:");
				jLabel3.setBounds(43, 204, 65, 21);
				jLabel3.setFont(new java.awt.Font("Segoe UI",1,16));
			}
			{
				ultimo = new JCheckBox();
				getContentPane().add(ultimo);
				ultimo.setText("Ultimo Jugador");
				ultimo.setBounds(43, 237, 174, 31);
				ultimo.setBackground(new java.awt.Color(217,236,255));
				ultimo.setFont(new java.awt.Font("Segoe UI",1,16));
			}
			{
				jLabel4 = new JLabel();
				getContentPane().add(jLabel4);
				jLabel4.setText("       Introduzca sus datos");
				jLabel4.setBounds(35, 12, 221, 22);
				jLabel4.setFont(new java.awt.Font("Segoe UI",3,16));
			}
			{
				nombre = new JTextField();
				getContentPane().add(nombre);
				nombre.setBounds(131, 55, 115, 22);
				nombre.setFont(new java.awt.Font("Segoe UI",0,14));
			}
			{
				SpinnerListModel puertoModel = 
						new SpinnerListModel(
								new String[] { "10001", "10002" , "10003" , "10004" , "10005" , "10006" , "10007","10008","10009" });
				puerto = new JSpinner();
				getContentPane().add(puerto);
				puerto.setModel(puertoModel);
				puerto.setBounds(131, 97, 111, 26);
				puerto.getEditor().setFont(new java.awt.Font("Dialog",0,14));
			}
			{
				btn_a = new JRadioButton();
				getContentPane().add(btn_a);
				btn_a.setText("A");
				btn_a.setBounds(131, 202, 45, 28);
				btn_a.setBackground(new java.awt.Color(217,236,255));
				btn_a.setFont(new java.awt.Font("Segoe UI",1,14));
				btn_a.setSelected(true);
			}
			{
				btn_b = new JRadioButton();
				getContentPane().add(btn_b);
				btn_b.setText("B");
				btn_b.setBounds(197, 204, 45, 25);
				btn_b.setBackground(new java.awt.Color(217,236,255));
				btn_b.setFont(new java.awt.Font("Segoe UI",1,14));
			}
			{
				alta = new JButton();
				getContentPane().add(alta);
				getContentPane().add(getJLabel5());
				getContentPane().add(getIp());
				alta.setText("Alta");
				alta.setBounds(99, 284, 77, 32);
				alta.setBackground(new java.awt.Color(0,0,0));
				alta.setFont(new java.awt.Font("Segoe UI",1,14));
				alta.setForeground(new java.awt.Color(255,255,255));
				alta.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
				alta.setIcon(new ImageIcon(getClass().getClassLoader().getResource("Imagenes/tick_verde.png")));
				alta.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						altaMouseClicked(evt);
					}
				});
			}
			pack();
			this.setSize(283, 374);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private ButtonGroup getGrupo() {
		if(grupo == null) {
			grupo = new ButtonGroup();
			grupo.add(btn_a);
			grupo.add(btn_b);
		}
		return grupo;
	}
	
	private void altaMouseClicked(MouseEvent evt) {
		String name=nombre.getText();
		String tcp=ip.getText();
		int port=Integer.parseInt((puerto.getValue().toString()));
		String grupo="A";
		InterfazJugador c=new InterfazJugador();
		boolean ult=false;
		if(btn_a.isSelected()) grupo="A";
		else if(btn_b.isSelected()) grupo="B";
		if(ultimo.isSelected()) ult=true;
		JuegoCliente j=new JuegoCliente(name,port,grupo,ult,tcp,c);
		if(j.ComenzarJugar()){
			c.setCliente(j);
			c.setTitle(j.getNombre());
			c.setVisible(true);
			dispose();
		}
	}
	
	private JLabel getJLabel5() {
		if(jLabel5 == null) {
			jLabel5 = new JLabel();
			jLabel5.setText("IP:");
			jLabel5.setBounds(43, 145, 27, 34);
			jLabel5.setFont(new java.awt.Font("Segoe UI",1,16));
		}
		return jLabel5;
	}
	
	private JTextField getIp() {
		if(ip == null) {
			ip = new JTextField();
			ip.setBounds(88, 153, 154, 23);
		}
		return ip;
	}

}
